<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/2
 * Time: 16:28
 */
namespace app\index\controller;
use think\Session;
use think\Db;
use think\View;
use think\Controller;
class Coupons  extends Base
{

    public function coupons()
    {
        //查询可用的优惠券
        $Login_userId=  Session::get('Login_userId');
        $canUse =  canUse($Login_userId);
        //过期优惠券
        $expiry =  Db::name('buyer_coupon')
            ->alias('a')
            ->join('coupon w','a.couponId = w.couponId')
            ->where(array('memberId'=>$Login_userId,'providerId'=>getProviderId(),'status'=>2,'w.isEnable' => 1))
            ->select();
        foreach($expiry  as $k1 => $v1){
            if($v1['categoryId']  == 0){
                $expiry[$k1]['categoryName'] ='全品';
                $addDateTime =   Db::name('buyer_coupon')->where(array('buyerCouponId'=>$v1['buyerCouponId']))->value('addDateTime');
                $expiry[$k1]['formTime'] =$addDateTime;
            }else{
                $categoryName =  Db::name('service_category')->where(array('serviceCategoryId'=>$v1['categoryId']))->value('categoryName');
                $expiry[$k1]['categoryName'] =$categoryName;
                $addDateTime =   Db::name('buyer_coupon')->where(array('buyerCouponId'=>$v1['buyerCouponId']))->value('addDateTime');
                $expiry[$k1]['formTime'] =$addDateTime;
            }
        }
        //已使用优惠券
        $used =  Db::name('buyer_coupon')
            ->alias('a')
            ->join('coupon w','a.couponId = w.couponId')
            ->where(array('memberId'=>$Login_userId,'providerId'=>getProviderId(),'status'=>1,'w.isEnable' => 1))
            ->select();
        foreach($used  as $k2 => $v2){
            if($v2['categoryId']  == 0){
                $used[$k2]['categoryName'] ='全品';
                $addDateTime =   Db::name('buyer_coupon')->where(array('buyerCouponId'=>$v2['buyerCouponId']))->value('addDateTime');
                $used[$k2]['formTime'] =$addDateTime;
            }else{
                $categoryName =  Db::name('service_category')->where(array('serviceCategoryId'=>$v2['categoryId']))->value('categoryName');
                $used[$k2]['categoryName'] =$categoryName;
                $addDateTime =   Db::name('buyer_coupon')->where(array('buyerCouponId'=>$v2['buyerCouponId']))->value('addDateTime');
                $used[$k2]['formTime'] =$addDateTime;
            }
        }
        $this->view->assign('expiry', $expiry);
        $this->view->assign('canUse', $canUse);
        $this->view->assign('used', $used);
        return $this->view->fetch();
    }
}